package com.shop2cn.iapi.sqoptrade.testcase;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.CancelOrderTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.BatchCancelOrderReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.BatchCancelOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 批量取消订单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqBatchCancelOrder {

	private static BatchCancelOrderReq  batchCancelOrderReq;
	private static BatchCancelOrderCall batchcancelordercall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long cdfMembergoodsOwnerId = ShenqiUserSet.cdfMembergoodsOwnerId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量取消订单");
	}

	@Before
	public void caseUp() {

		batchCancelOrderReq = new BatchCancelOrderReq();
		batchcancelordercall = new BatchCancelOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchcancelordercall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchCancelOrder_001() {
		Logger.start(true, "订单支付前,买家批量取消订单成功,订单状态为13 卖家取消订单");
		try {
			// 参数
			Logger.comment("前置数据准备");
			ArrayList<Long> orderIdList = new ArrayList<Long>();

			long sellerId = cdfMembergoodsOwnerId;

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createMultiCodeProxychain((int) cdfMembergoodsOwnerId, false);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOneMainOrderMultiSubOrderWithCDFMember(productBean, cdfMembergoodsOwnerId,
							10L);
			
			for (int i = 0; i < placeOrderResp.getOrderList().size(); i++) {
				
				orderIdList.add(placeOrderResp.getOrderList().get(i).getOrderId());
			}
			
			Logger.comment("前置数据准备结束");
			batchCancelOrderReq.setOrderIdList(orderIdList);
			batchCancelOrderReq.setCancelReason("自动化测试取消订单");
			batchCancelOrderReq
					.setCancelType(CancelOrderTypeEnum.SELLER_CANCEL_ORDER);
			// 卖家ID
			batchCancelOrderReq.setUserId(sellerId);
			// 发送请求
			batchcancelordercall.setData(batchCancelOrderReq);
			batchcancelordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchcancelordercall.getCode(),
					"验证Code");
			
			// 验证数据库
			int size = orderIdList.size();
			for (int i = 0; i < size; i++) {
				long orderId = batchCancelOrderReq.getOrderIdList().get(i);

				Ts_SqCancelOrder.verifyOrderStatus(13, orderId,
						batchCancelOrderReq.getUserId(),
						batchCancelOrderReq.getCancelType(),
						batchCancelOrderReq.getCancelReason());
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
